package com.atguigu.gulimail.ware.service.impl;

import jdk.nashorn.internal.ir.CallNode;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimail.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimail.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //初始化查询条件实例
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        //请求参数中进行获取key status skuId purchaseId
        String key = (String) params.get("key");
        //判断如果检索条件key不为空
        if(!StringUtils.isEmpty(key)){
            //构造条件
            wrapper.and((w)->{
               w.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }
        //从请求参数中进行获取status
        String status =  (String)params.get("status");
        //进行判断status如果不为空
        if(!StringUtils.isEmpty(status)){
            wrapper.and((w) ->{
                w.eq("status",status);
            });
        }

        //构造一个wareId条件
        //从请求参数中进行获取wareId
        String wareId = (String)params.get("wareId");
        //进行判断如果wareId不为空进行处理
        if(!StringUtils.isEmpty(wareId)){
            wrapper.and( (w) ->{
                w.eq("ware_id",wareId);
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    //根据采购单id获取采购项
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        //根据查询条件进行查询
        List<PurchaseDetailEntity> purchaseDetailEntities = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return purchaseDetailEntities;
    }

}